package com.cskaoyan.order.biz.handler;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.util.NumberUtils;
import com.cskaoyan.order.biz.callback.SendEmailCallback;
import com.cskaoyan.order.biz.callback.TransCallback;
import com.cskaoyan.order.biz.context.AbsTransHandlerContext;
import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.constant.ItemStatusConstants;
import com.cskaoyan.order.constant.OrderConstants;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dto.CartProductDto;
import com.cskaoyan.order.utils.GlobalIdGeneratorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 初始化订单 生成订单
 */

@Slf4j
@Component
public class InitOrderHandler extends AbstractTransHandler {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    GlobalIdGeneratorUtil globalIdGeneratorUtil;

    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        //int i = 1/0;

        // 插入一条新的订单数据
        CreateOrderContext createOrderContext = (CreateOrderContext) context;

        Order order = new Order();

        // 通过发号器生成一个唯一的订单id
        String orderId = globalIdGeneratorUtil.getNextSeq("orderId", 1);

        createOrderContext.setOrderId(orderId);

        order.setOrderId(orderId);
        order.setPayment(createOrderContext.getOrderTotal());
        order.setStatus(OrderConstants.ORDER_STATUS_INIT);
        Date date = new Date();
        order.setCreateTime(date);
        order.setUpdateTime(date);
        order.setUserId(createOrderContext.getUserId());
        order.setBuyerNick(createOrderContext.getBuyerNickName());
        order.setUniqueKey(createOrderContext.getUniqueKey());
        order.setPayment(createOrderContext.getOrderTotal());

        orderMapper.insertSelective(order);

        // 插入订单-商品关联表

        List<CartProductDto> cartProductDtoList = ((CreateOrderContext) context).getCartProductDtoList();

        for (CartProductDto cartProductDto : cartProductDtoList) {
            String orderItemId = globalIdGeneratorUtil.getNextSeq("orderItemId", 1);

            OrderItem orderItem = new OrderItem();
            orderItem.setId(orderItemId);
            orderItem.setItemId(cartProductDto.getProductId());
            orderItem.setOrderId(orderId);
            orderItem.setNum(cartProductDto.getProductNum().intValue());
            orderItem.setTitle(cartProductDto.getProductName());
            orderItem.setPrice(NumberUtils.toDouble(cartProductDto.getSalePrice()));

            BigDecimal productNum = new BigDecimal(cartProductDto.getProductNum());
            orderItem.setTotalFee(NumberUtils.toDouble(productNum.multiply(cartProductDto.getSalePrice())));

            orderItem.setPicPath(cartProductDto.getProductImg());
            // 锁定库存
            orderItem.setStatus(ItemStatusConstants.STOCK_LOCK);

            orderItemMapper.insertSelective(orderItem);

        }
        return true;
    }

}
